package com.instance.demo5;


import javax.swing.*;
import java.awt.*;

/**
 * 可设置背景图片的JPanel，提供了三种显示背景图片的方式：居中、平铺和拉伸。
 * 未设置背景图片的情况下，同JPanel。
 */
public class ImgPanel extends JComponent
{
    private static final long serialVersionUID = 1L;
    private Image backgroundImage;
    private int imageDisplayMode;
    private int modeIndex;
    public ImgPanel()
    {
        this(null, 0);
    }
    public ImgPanel(Image image, int modeName)
    {
        super();
        setBackgroundImage(image);
        setImageDisplayMode(modeName);
    }
    public void setBackgroundImage(Image image)
    {
        this.backgroundImage = image;
        this.repaint();
    }
    public Image getBackgroundImage()
    {
        return backgroundImage;
    }
    public void setImageDisplayMode(int modeName)
    {
                  //居中
            if(modeName==1)
            {

                modeIndex = 0;
            }
            //平铺
        if(modeName==0)
        {
            this.imageDisplayMode = 0;
            modeIndex = 1;
        }
            //拉伸
        if(modeName==2)
        {
            this.imageDisplayMode = 2;
            modeIndex = 2;
        }
        if(modeName==4)
        {
            this.imageDisplayMode = 4;
            modeIndex = 3;
        }
            this.repaint();
        }

    public int getImageDisplayMode()
    {
        return imageDisplayMode;
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        //如果设置了背景图片则显示
        if(backgroundImage != null)
        {
            int width = this.getWidth();
            int height = this.getHeight();
            int imageWidth = backgroundImage.getWidth(this);
            int imageHeight = backgroundImage.getHeight(this);

            switch(modeIndex)
            {
                //居中
                case 0:
                {
                    int x = (width - imageWidth) / 2;
                    int y = (height - imageHeight) / 2;
                    g.drawImage(backgroundImage, x, y, this);
                    break;
                }
                //平铺
                case 1:
                {
                    for(int ix = 0; ix < width; ix += imageWidth)
                    {
                        for(int iy = 0; iy < height; iy += imageHeight)
                        {
                            g.drawImage(backgroundImage, ix, iy, this);
                        }
                    }

                    break;
                }
                //拉伸
                case 2:
                {
                    g.drawImage(backgroundImage, 0, 0, width, height, this);
                    break;
                }
                case 3:

                    double sx = (double) width / imageWidth;
                    double sy = (double) height / imageHeight;
                    // 这里想实现在targetW，targetH范围内实现等比缩放。如果不需要等比缩放
                    // 则将下面的if else语句注释即可
                    if (sx > sy) {
                        sx = sy;
                        width = (int) (sx * imageWidth);
                    } else {
                        sy = sx;
                        height = (int) (sy * imageHeight);
                    }
                    int x = (this.getWidth() - width) / 2;
                    int y = (this.getHeight() - height) / 2;
                    g.drawImage(backgroundImage, x, y, width, height, this);
                    break;
            }
        }
    }
    
public static void main(String[] args) {
	new ImgPanel();
}

}

